//
//  FDLoginView.m
//  FamousDoctorOnline
//
//  Created by wszyxc on 16/7/6.
//  Copyright © 2016年 wszyxc. All rights reserved.
//

#import "FDLoginView.h"
#import "WZHUserDefaults.h"



@interface FDLoginView ()

//用户头像
@property (weak,nonatomic) UIImageView * userImageView;
//用户名
@property (weak,nonatomic) UILabel * userNameLabel;
//用户信息label
@property (weak,nonatomic) UIButton * userInfoButton;


//手势

@property (weak,nonatomic) UITapGestureRecognizer * tap;

@end


@implementation FDLoginView



- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        [self setupUI];
        
        NSArray *userInfo = [WZHUserDefaults WZHObjectForKey:@"userInfo"];
        
        //取出用户姓名
        NSString *getName = userInfo[kUserInfoName];

        
        self.userNameLabel.text = getName;
        
    }
    return self;
}

- (void)layoutSubviews{
    
    [super layoutSubviews];
    
    [self setupUserImageViewLayout];
    
    [self setupUserNameLabelLayout];
    
    [self setupUserInfoLabelLayout];
    
}



- (void)setupUI{
    
    //1.设置headerView
   
    self.backgroundColor = kThemeColor;
    
    //2.实例化头像view
    UIImageView *userImageView = [[UIImageView alloc]init];
    
    self.userImageView = userImageView;
    
    userImageView.image = [[UIImage imageNamed:@"banner"] circleImage];
    
    NSLog(@"%@",NSStringFromCGSize(userImageView.image.size));
    
    //添加头像
    [self addSubview:userImageView];
    
    //添加手势
    
    userImageView.userInteractionEnabled = YES;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(userInfoClick)];
    
    self.tap = tap;
    
    tap.numberOfTouchesRequired = 1;
    
    tap.numberOfTapsRequired = 1;
    
    [userImageView addGestureRecognizer:tap];
    
   
    
    
    //3.实例化userNameLabel
    UILabel *userNameLabel = [[UILabel alloc]init];
    
    self.userNameLabel = userNameLabel;
    
    NSString *userName = [WZHUserDefaults WZHStringForKey:@"userName"];
    
    if (!userName) {
        
        userName = @"用户名";
        
    }
    
    userNameLabel.text = userName;
    
    [self addSubview:userNameLabel];
    
    //4.实例化userInfoLabel
    UIButton *userInfoButton = [[UIButton alloc]init];
    
    self.userInfoButton = userInfoButton;
    
    [userInfoButton setTitle:@"查看个人资料" forState:UIControlStateNormal];
    
    [self addSubview:userInfoButton];
    
    [userInfoButton addTarget:self action:@selector(userInfoClick) forControlEvents:UIControlEventTouchUpInside];
    
    
    
}




/**
 *  点击了获取用户资料
 */

- (void)userInfoClick{
    
   // NSLog(@"获取用户资料");
   
    
    if (self.loginViewBlock) {
      //  NSLog(@"调用了block");
        self.loginViewBlock();
    }
    
    
}


- (void)setupUserImageViewLayout{
    
    [self.userImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.equalTo(self.mas_leading).offset(20);
        make.width.equalTo(self.userImageView.mas_height);
        
        make.top.offset(20);
        
        make.bottom.offset(-20);
        
    }];
    
}

- (void)setupUserNameLabelLayout{
    
    [self.userNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    
            make.top.offset(25);
            make.leading.equalTo(self.userImageView.mas_trailing).offset(10);
    
        }];
   
}

- (void)setupUserInfoLabelLayout{
    
    [self.userInfoButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.equalTo(self.userImageView.mas_trailing).offset(10);
        make.bottom.offset(-25);
        
    }];
    
}


- (void)dealloc{
    
     [self.userImageView removeGestureRecognizer:self.tap];
}



@end
